<template>
  <el-input
    style="width: 100%"
    v-model="selectName"
    :disabled="disabled"
    :placeholder="placeholder"
  >
    <template #append>
      <el-button icon="Promotion" @click="handlePicker" />
    </template>
  </el-input>
  <!-- 添加或修改用户配置对话框 -->
  <el-dialog :title="title" :close-on-click-modal="false" v-model="open" width="70%" append-to-body>
    <div class="app-container">
      <!--用户数据-->
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        label-width="100px"
      >
        <el-form-item
          v-for="(el, index) in columns"
          :key="index"
          :label="el.label"
          :prop="el.prop"
        >
          <el-input
            v-model="queryParams[el]"
            placeholder=""
            clearable
            style="width: 240px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @current-change="handleCurrentChange"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          v-if="multiple"
          type="selection"
          width="50"
          align="center"
        />
        <el-table-column
          v-for="(el, index) in columns"
          :key="index"
          :label="el.label"
          :prop="el.prop"
          align="center"
        />
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </div>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="UserSelect">
import {
  workshopColumn,
  prodLineColumn,
  prodSectionColumn,
  workStationColumn,
  storehouseColumn,
  binColumn,
} from "./columns";
import { listWorkshop } from "@/api/base/workshop";
import { listPage } from "@/api/base/prodLine";
import { listProdSection } from "@/api/base/processSection";
import { listWorkStation } from "@/api/base/workstation";
import { listStorehouse } from "@/api/storehouse/storehouse";
import { listStorehouseBin } from "@/api/storehouse/storehouseBin";
import { toRefs } from "vue";

const props = defineProps({
  title: {
    type: String,
    default: "",
  },
  multiple: {
    type: Boolean,
    default: false,
  },
  disabled: {
    type: Boolean,
    default: false,
  },
  type: {
    type: String,
    default: "",
  },
  placeholder: {
    type: String,
    default: "",
  },
  parent: {
    type: String,
    default: "",
  },
  factory: {
    type: String,
    default: "",
  },
  workshop: {
    type: String,
    default: "",
  },
  productLine: {
    type: String,
    default: "",
  },
  sectionCode: {
    type: String,
    default: "",
  },
});

const { title, multiple, type, factory, workshop, disabled, productLine,sectionCode } =
  toRefs(props);
const { proxy } = getCurrentInstance();

const open = ref(false);
const selectName = defineModel();
const selectValue = defineModel("code");

const emit = defineEmits(["change"]);
const tableData = ref([]);
const selectionData = ref([]);
const loading = ref(true);
const ids = ref([]);
const single = ref(true);

const total = ref(0);
const selectData = ref([]);
const currentRow = ref();

const queryParams = ref({});
const columns = computed(() => {
  if (type.value === "workshop") return workshopColumn;
  if (type.value === "prodLine") return prodLineColumn;
  if (type.value === "prodSection") return prodSectionColumn;
  if (type.value === "workStation") return workStationColumn;
  if (type.value === "storehouse") return storehouseColumn;
  if (type.value === "bin") return binColumn;
  return [];
});
const api = computed(() => {
  if (type.value === "workshop") return listWorkshop;
  if (type.value === "prodLine") return listPage;
  if (type.value === "prodSection") return listProdSection;
  if (type.value === "workStation") return listWorkStation;
  if (type.value === "storehouse") return listStorehouse;
  if (type.value === "bin") return listStorehouseBin;
  return [];
});
const codeField = computed(() => {
  if (type.value === "workshop") return "workshopCode";
  if (type.value === "prodLine") return "code";
  if (type.value === "prodSection") return "sectionCode";
  if (type.value === "workStation") return "workStationCode";
  if (type.value === "storehouse") return "storehouseCode";
  if (type.value === "bin") return "binCode";
  return [];
});
const nameField = computed(() => {
  if (type.value === "workshop") return "workshopName";
  if (type.value === "prodLine") return "name";
  if (type.value === "prodSection") return "sectionName";
  if (type.value === "workStation") return "workStationName";
  if (type.value === "storehouse") return "storehouseName";
  if (type.value === "bin") return "binName";
  return [];
});
const filterValue = computed(() => {
  if (type.value === "workshop") return "factory";
  if (type.value === "prodLine") return "workshopCode";
  if (type.value === "prodSection") return "productLine";
  if (type.value === "workStation") return "prodSection";
  if (type.value === "storehouse") return "sectionCode";
  if (type.value === "bin") return "storehouseCode";
  return [];
});

const handleCurrentChange = (val) => {
  if (!multiple.value) {
    currentRow.value = val;
  }
};

function handlePicker() {
  if (disabled.value) return;
  open.value = true;
  getList();
}

function submitForm() {
  if (!multiple.value) {
    selectName.value = currentRow.value[nameField.value];
    selectValue.value = currentRow.value[codeField.value];
    open.value = false;
    emit("change", currentRow.value);
    return;
  }
  selectName.value = selectData.value.join(",");
  selectValue.value = ids.value.join(",");
  open.value = false;
  emit("change", selectionData.value);
}
function cancel() {
  open.value = false;
}

/** 查询用户列表 */
function getList() {
  loading.value = true;
  if (factory.value) {
    queryParams.value.factory = factory.value;
  }
  if (workshop.value) {
    queryParams.value.workshop = workshop.value;
  }
  if (productLine.value) {
    queryParams.value.productLine = productLine.value;
  }
  if (sectionCode.value) {
    queryParams.value.sectionCode = sectionCode.value;
  }
  api.value(queryParams.value).then((res) => {
    loading.value = false;
    tableData.value = res.rows;
    total.value = res.total;
  });
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.deptId = undefined;
  queryParams.value.status = userStatus;
  proxy.$refs.deptTreeRef.setCurrentKey(null);
  handleQuery();
}

/** 选择条数  */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item[codeField.value]);
  single.value = selection.length != 1;
  selectionData.value = selection;
  selectData.value = selection.map((item) => item[nameField.value]);
}

</script>
